package com.twfpro.utils;

import java.io.FileReader;
import java.io.Reader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.json.JSONArray;

/**
 * CSV文件工具
 * 
 * @author 高文
 *
 */
public class CsvUtils
{
	/**
	 * 读取CSV文件内容
	 * 
	 * @param path
	 *            文件全路径
	 * @return json数组，例：[["123","321"],["abc","cba"]]
	 */
	public static JSONArray readCsvContents(String path)
	{
		JSONArray contents = new JSONArray();
		try
		{
			Reader reader = new FileReader(path);
			Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader);
			for (CSVRecord record : records)
			{
				JSONArray content = new JSONArray();
				for (String str : record)
				{
					content.put(str);
				}
				contents.put(content);
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return contents;
	}
}
